import { apiClient } from '@/lib/api-client';
import type { ApiResponse } from '@/lib/types/api';

interface Mood {
  _id: string;
  userId: string;
  createdAt: Date;
  mood: {
    level: 1 | 2 | 3 | 4 | 5;
  };
  remark: string;
}

interface CreateMoodRequest {
  mood: {
    level: 1 | 2 | 3 | 4 | 5;
  };
  remark: string;
}

interface UpdateMoodRequest {
  mood?: {
    level: 1 | 2 | 3 | 4 | 5;
  };
  remark?: string;
}

export class MoodService {
  // 获取所有心情记录
  static async getMoods(): Promise<ApiResponse<Mood[]>> {
    return await apiClient<null, Mood[]>('/mood', {
      method: 'GET',
    });
  }

  // 创建心情记录
  static async createMood(data: CreateMoodRequest): Promise<ApiResponse<Mood>> {
    return await apiClient<CreateMoodRequest, Mood>('/mood', {
      method: 'POST',
      body: data,
    });
  }

  // 获取单个心情记录
  static async getMood(moodId: string): Promise<ApiResponse<Mood>> {
    return await apiClient<null, Mood>(`/mood/${moodId}`, {
      method: 'GET',
    });
  }

  // 更新心情记录
  static async updateMood(moodId: string, data: UpdateMoodRequest): Promise<ApiResponse<Mood>> {
    return await apiClient<UpdateMoodRequest, Mood>(`/mood/${moodId}`, {
      method: 'PUT',
      body: data,
    });
  }

  // 删除心情记录
  static async deleteMood(moodId: string): Promise<ApiResponse<void>> {
    return await apiClient<null, void>(`/mood/${moodId}`, {
      method: 'DELETE',
    });
  }

  // 获取心情统计数据
  static async getMoodStats(): Promise<ApiResponse<{
    total: number;
    byLevel: Record<number, number>;
    recentTrend: {
      date: string;
      level: number;
    }[];
  }>> {
    return await apiClient<null, {
      total: number;
      byLevel: Record<number, number>;
      recentTrend: {
        date: string;
        level: number;
      }[];
    }>('/mood/stats', {
      method: 'GET',
    });
  }
}

export type { Mood, CreateMoodRequest, UpdateMoodRequest };
